<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14
 * Time: 12:13
 */
class inside
{
    public static function start()
    {
        /*加载配置*/
        self::config();
        /*加载通用参数设置*/
        self::common();
        /*加载异常记录器*/
        self::registerException();
        //加载twig视图模板
        self::view();
        /*路由调度*/
        self::Dispatcher();
    }

    protected static function Dispatcher()
    {
        Sys\Library\Dispatcher::run();
    }

    protected static function common()
    {
        date_default_timezone_set(config('app.timezone'));
        //定义日志目录
        if(strpos(config('app.logs_path'), '/') === 0) {
            define('SGLOGS_PATH', config('app.logs_path'));
        } else {
            define('SGLOGS_PATH', APP_ROOT . config('app.logs_path'));
        }

    }

    /**
     * 初使twig模板引擎
     */
    protected static function view()
    {
        global $twig;
        $loader = new Twig_Loader_Filesystem(APP_ROOT . config('view.path', null, __DIR__ .'/'));
        $options = array(
            'debug' => config('view.debug', null, false),
            'autoescape' => config('view.autoescape', null, 'html'),
        );
        if(config('view.cache')) {
            $options['cache'] = APP_ROOT . config('view.cache_path', null, __DIR__ . '/');
        }
        $twig = new Twig_Environment($loader, $options);
    }

    protected static function config()
    {
        if ($configs = glob(APP_CONFIG_PATH . '*.php')) {
            $list = array();
            foreach ($configs as $config) {
                $path                    = pathinfo($config);
                $list[$path['filename']] = include($config);
            }
            config($list);
        }
    }

    /**
     * 加载注册方法
     */
    protected static function registerException()
    {
        set_exception_handler('_exceptionHandle');
        set_error_handler('_errorHandle');
        register_shutdown_function('_shutdowHandle');
    }

}